/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package backingBean;

import entities.Members;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.Part;

/**
 *
 * @author ambaye
 */
@ManagedBean
@RequestScoped
public class ProfileMB implements Serializable{
    
@ManagedProperty(value = "#{login.members}")
 private Members unmembre;
private Part profileImage;
//    public void setProfilevisite(Members profilevisite) {
//        this.profilevisite = profilevisite;
//    }
//
//    public boolean isIsConnected() {
//        return isConnected;
//    }
//
//    public void setIsConnected(boolean isConnected) {
//        this.isConnected = isConnected;
//    }

    public Members getUnmembre() {
        return unmembre;
    }

    public void setUnmembre(Members unmembre) {
        this.unmembre = unmembre;
    }

    /**
     * Creates a new instance of ProfileMB
     */
    public ProfileMB() {
    }
    @PostConstruct
    public void InitData(){   
       System.out.println("test1");
       
     unmembre= new Members();
     FacesContext contextafficher = FacesContext.getCurrentInstance();
        unmembre = (Members)contextafficher.getExternalContext().getSessionMap().get("member");       
    }
    
    public String upload() throws IOException {  
         InputStream inputStream = profileImage.getInputStream();          
        FileOutputStream outputStream = new FileOutputStream(getFilename(profileImage));  
          
        byte[] buffer = new byte[4096];          
        int bytesRead = 0;  
        while(true) {                          
            bytesRead = inputStream.read(buffer);  
            if(bytesRead > 0) {  
                outputStream.write(buffer, 0, bytesRead);  
            }else {  
                break;  
            }                         
        }  
        outputStream.close();  
        inputStream.close();  
         
        return "success";  
    }  
  
    private static String getFilename(Part part) {  
        for (String cd : part.getHeader("content-disposition").split(";")) {  
            if (cd.trim().startsWith("filename")) {  
                String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");  
                //return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\') + 1); // MSIE fix.  
            }  
        }  
        return null;  
    }
//    public String afficher(int myid){
//        FacesContext contextafficher = FacesContext.getCurrentInstance();
//        unmembre = (Members)contextafficher.getExternalContext().getSessionMap().get("members");
//         
//         
//        
//      return unmembre;  
//    }
//            
//            
//        }

}

//    public boolean afficher (boolean status){       
//       
//    
//       String retour=null;
//       if (status==true){
//            retour ="/images/o.png";        
//   }
//      else
//      {
//          retour="/images/f.png";
//       }
//       return status;
//          
//       }

